package com.zzh.camera2.ui

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.zzh.camera2.R
import com.zzh.camera2.model.CameraParameter
import kotlinx.android.synthetic.main.parameter_fragment.*
import java.lang.Exception

class CameraParameterActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.camera_parameter_activity)
        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, ParameterFragment())
                .commit()
        }
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    class ParameterFragment : Fragment() {

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.parameter_fragment, null)
        }

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            initView()
        }

        private fun initView() {
            editAboveCameraId.requestFocus()
            // 设置默认参数
            setDefaultApiVersion()
            setIsShowPreviewView(true)
            setIsShowCallbackImage(true)

            btnGotoMainActivity.setOnClickListener(View.OnClickListener {
                try {
                    CameraParameter.useApiVersion = if (rbApi1.isChecked)
                        CameraParameter.API_VERSION_1
                    else
                        CameraParameter.API_VERSION_2

                    CameraParameter.isShowPreviewView = rbShowPreview.isChecked
                    CameraParameter.isShowCallbackImage = rbShowCallbackImage.isChecked

                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
                        && CameraParameter.useApiVersion == CameraParameter.API_VERSION_2
                    ) {
                        CameraParameter.useApiVersion = CameraParameter.API_VERSION_1
                        rbApi1.isChecked = true
                        Toast.makeText(activity, "当前系统版本过低，只能使用Api1打开相机", Toast.LENGTH_SHORT).show()
                        return@OnClickListener
                    }

                    var isId1Default = true
                    var isId2Default = true;
                    var cameraId1 = editAboveCameraId.hint.toString()
                    var cameraId2 = editBelowCameraId.hint.toString()
                    if (!TextUtils.isEmpty(editAboveCameraId.text)) {
                        isId1Default = false
                        cameraId1 = editAboveCameraId.text.toString()
                    }
                    if (!TextUtils.isEmpty(editBelowCameraId.text)) {
                        isId2Default = false
                        cameraId2 = editBelowCameraId.text.toString()
                    }

                    if (cameraId1.trim() == cameraId2.trim()) {
                        Toast.makeText(activity, "两个相机ID不能相同，请重新设置", Toast.LENGTH_SHORT).show()
                        return@OnClickListener
                    }

                    CameraParameter.aboveCameraId = Integer.valueOf(cameraId1)
                    CameraParameter.belowCameraId = Integer.valueOf(cameraId2)
                    activity?.startActivity(Intent(activity, MainActivity::class.java))

                    if (isId1Default && isId2Default) {
                        Toast.makeText(activity, "使用默认相机ID", Toast.LENGTH_LONG).show()
                    }
                } catch (e: Exception) {
                    Log.e("ParameterFragment", "initView start main activity error e=" + e.message)
                    e.printStackTrace()
                }
            })

        }

        private fun setDefaultApiVersion() {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                rbApi1.isChecked = true
            } else {
                rbApi2.isChecked = true
            }
        }

        private fun setIsShowPreviewView(show: Boolean) {
            if (show) {
                rbShowPreview.isChecked = true
            } else {
                rbNotShowPreview.isChecked = true
            }
        }

        private fun setIsShowCallbackImage(show: Boolean) {
            if (show) {
                rbShowCallbackImage.isChecked = true
            } else {
                rbNotShowCallbackImage.isChecked = true
            }
        }
    }
}